$(function() {
	var input = document.getElementById("images");
	var	formdata = new FormData();
	function showUploadedItem (source) {
  		var list = document.getElementById("gallery"),
	  		a   = document.createElement("a"),
	  		img  = document.createElement("img");
  		img.src = source;
  		img.width = 230;
  		img.height = 150;
  		a.appendChild(img);
		list.appendChild(a);
	};
	$("#btn").click(function(){
 		document.getElementById("response").innerHTML = "Uploading . . .";
 		var len = input.files.length, reader, file;
		
		for ( i=0; i < len; i++ ) {
			file = input.files[i];
	
			if (!!file.type.match(/image.*/)) {
				if ( window.FileReader ) {
					reader = new FileReader();
					reader.onloadend = function (e) { 
						showUploadedItem(e.target.result, file.fileName);
					};
					reader.readAsDataURL(file);
				}
				if (formdata) {
					formdata.append("images[]", file);
				}
			}	
		}

		if (formdata) {
			$.ajax({
				url: "lib/upload.php",
				type: "POST",
				data: formdata,
				processData: false,
				contentType: false,
				success: function (res) {
					$("#response").text(res);
				}
			});
		}
	});
});
